//
//  HDSettingViewController.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDSettingViewController: UIViewController {
    
    let setTableView = UITableView(frame: CGRect.zero, style: .grouped)
    var titles = [String]()
    private var accountInfo:HDAccountInfo?
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0xf8f8f8)
        title = "设置"
        setTableView.backgroundColor = UIColor(hex: 0xf8f8f8)
        setTableView.rowHeight = 49
        setTableView.isScrollEnabled = false
        setTableView.separatorStyle = .none
        view.addSubview(setTableView)
        setTableView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(150)
        }
        setTableView.dataSource = self
        setTableView.delegate = self
        setTableView.register(UITableViewCell.self, forCellReuseIdentifier: "settingCell")
        
        
        if HDAccountHandler.loginType == .notLogin {
            titles = ["关于我们"]
        } else {
            titles = ["修改密码","关于我们"]
            setupSignOutButton()
        }
     
    }
    
    func setupSignOutButton() {
        if HDAccountHandler.loginType == .notLogin {
            return
        }else {
            
            let signOutButton = HDNextStepButton(title: "退出登录")
            signOutButton.addTarget(self, action: #selector(signOutAction), for: .touchUpInside)
            self.view.addSubview(signOutButton)
            signOutButton.snp.makeConstraints({ (make) in
                make.left.equalTo(37)
                make.right.equalTo(-37)
                make.top.equalTo(setTableView.snp.bottom).offset(50)
                make.height.equalTo(40)
            })
        }
        
    }
    func signOutAction() {
        HDAccountHandler.signOut()
        navigationController?.popToRootViewController(animated: true)
        
    }
    
}


extension HDSettingViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingCell", for: indexPath)
        cell.backgroundColor = UIColor.white
        cell.accessoryType = .disclosureIndicator
        cell.textLabel?.text = titles[indexPath.row]
        cell.textLabel?.textColor = UIColor(hex: 0x666666)
        cell.selectionStyle = .none
        let line1 = UILabel(frame: CGRect(x: 0,y: 0,width: AppConstant.screenWidth,height: 0.5))
        line1.backgroundColor = UIColor(hex: 0xdbdbdb)
        cell.addSubview(line1)
        let line2 = UILabel()
        line2.backgroundColor = UIColor(hex: 0xdbdbdb)
        cell.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(0.5)
        }
        if indexPath.row == 1 {
            line1.isHidden = true
        }else {
            line1.isHidden = false
        }
        return cell
    }
}

extension HDSettingViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if HDAccountHandler.loginType == .notLogin {
            navigationController?.pushViewController(HDAboutUsViewController(), animated: true)
        } else {
            if indexPath.row == 0 {
                navigationController?.pushViewController(HDEditPasswordViewController(), animated: true)
                
            } else {
                navigationController?.pushViewController(HDAboutUsViewController(), animated: true)
                
            }
        }
    }
}
